import { Component, OnInit } from '@angular/core';
import { GridOptions } from '@ag-grid-community/core';
import { GridConfigService } from '@core/config/grid-config.service';
import { ApiService } from '@core/services/http/api.service';
import { UserAuthService } from '@core/services/user-auth.service';
import { DatePipe } from '@angular/common';
import {ApesMessageService} from "@shared/apes/message";

@Component({
  selector: 'complaint',
  templateUrl: './complaint.component.html',
  styles: [`
    * {
      -webkit-overflow-scrolling: touch;
      -webkit-transform: translateZ(0);
    }
  `],
})
export class ComplaintComponent implements OnInit {
  /* 按钮相关 */
  buttonBar = [
    { hide: true, style: 'default', label: '刷新', id: 'action_query', loading: false },
  ];
  title = '';
  ctx = { model: {} };

  /** Grid 相关 **/
  rowData: any[] = [];
  resultData: any;
  gridOptions: GridOptions;
  pinnedBottomRowData: any[] = [];
  columnDefs: any[] = [];
  getContextMenuItems: any;
  height = 275 + 'px';
  posts: any;                // 获取登录角色所在门店ID

  constructor(private apiService: ApiService,
              private gridConfigService: GridConfigService,
              private userAuthService: UserAuthService,
              private datePipe: DatePipe , private msg: ApesMessageService) {

    const userInfo = this.userAuthService.getUserInfoObj();
    if(userInfo.userAccount.post.branchObj !== undefined) {
      this.posts = userInfo.userAccount.post.branchObj.origin;
      // if(userInfo.userAccount.post.dept !== undefined) {
      //   this.posts = userInfo.userAccount.post.dept.id;
    }else {
      this.posts = null;
    }

    this.columnDefs = this.reportList[0].titleList;

    // 初始化 grid
    this.gridOptions = <GridOptions>{
      toolPanelSuppressSideButtons: true,
      suppressExcelExport: true,
      suppressCsvExport: true,
      suppressSizeToFit: true,
      showToolPanel: false,
      pagination: true,
      paginationPageSize: 6,
      rowHeight: 35,
    };
    this.gridOptions.localeText = gridConfigService.getLocaleText();

  }

  ngOnInit() {
    this.getTablesData(this.posts);
  }

  /**
   * 功能：按钮事件路由
   * @param data 当前按钮元数据
   */
  onEvent(data: any) {

    if (data instanceof MouseEvent) return;
    if (data) {
      if (data.id === 'action_query') {
        this.buttonBar[0].loading = true;
        this.queryData();
      }
    }
  }

  queryData() {
    this.rowData = [];
    this.getTablesData(this.posts);
  }

  reportList = [
    {
      width: '100%',
      title: '客诉',
      titleList: [
        { headerName: '车牌', field: 'PLATE_NUMBER', width: 85 },
        { headerName: '录入时间', field: 'CREATE_DATE', width: 110 },
        { headerName: '跟踪人', field: 'TRACER_NAME', width: 80 },
        { headerName: '状态', field: 'CURRENT_NODE', width: 180 },
        { headerName: '内容', field: 'COMPLAIN_CONTENT', width: 330 },
      ],
    },
  ];

  getTablesData(posts: string) {
    let data = { 'BRANCH_ID': posts };
    this.apiService.post('crm.customComplain.queryReport', { 'body': JSON.stringify(data) }).subscribe(
      (body) => {
        if (body) {
          for (let i = 0, len = body.length; i < len; i++) {
            body[i]['CREATE_DATE'] = this.datePipe.transform(new Date(body[i]['CREATE_DATE']),
              'yyyy-MM-dd HH:mm:ss').slice(5, 19);
          }

          this.rowData = body;
          if( this.rowData.length === 0 ) {
            this.msg.info(`未找到满足条件的数据`);
          }
          this.buttonBar[0].loading = false;

        } else {
          this.msg.info(body.msg);
        }
      });
  }

}
